package com.lkd.handler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.google.common.base.Strings;
import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.OrderCheck;
import com.lkd.emq.Topic;
import com.lkd.entity.OrderEntity;
import com.lkd.service.OrderService;
import com.lkd.utils.JsonUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

@Component
@Topic(TopicConfig.ORDER_CHECK_TOPIC)
public class OrderCheckHandler implements MsgHandler {

    @Resource
    private OrderService orderService;


    @Override
    public void process(String jsonMsg) throws IOException {
        //接收消息，转换为协议类对象
        OrderCheck orderCheck = JsonUtil.getByJson(jsonMsg, OrderCheck.class);

        if (orderCheck == null || Strings.isNullOrEmpty(orderCheck.getOrderNo())) {
            return;
        }
        //查询订单
        QueryWrapper<OrderEntity> qw = new QueryWrapper<>();
        qw
                .lambda()
                .eq(OrderEntity::getOrderNo, orderCheck.getOrderNo())
                .eq(OrderEntity::getStatus, VMSystem.ORDER_STATUS_CREATE);

        OrderEntity orderEntity = orderService.getOne(qw);

        if (orderEntity == null) {
            return;
        }
        //修改订单状态
        UpdateWrapper<OrderEntity> uw = new UpdateWrapper<>();
        uw
                .lambda()
                .eq(OrderEntity::getOrderNo, orderCheck.getOrderNo())
                .set(OrderEntity::getStatus, VMSystem.ORDER_STATUS_INVALID);

        orderService.update(uw);

    }
}
